// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_ID_MANAGER_H__
#define __INCLUDED_CBL_ID_MANAGER_H__

#include "CBL_Defines.h"
#include "CBL_CRC32.h"

#include <vector>
#include <string>
#include <set>

CB_NAMESPACE_START

typedef int32 IdType;
static const IdType InvalidId = -1;
static const std::string InvalidIdString("InvalidId");

class CIdItem;

// ---------------------------------------------------------------------------------------------
//	CIdMap
//	Provides a basic id<->name maping interface with fast lookup routines for both sides.
//	Names are being hashed, so a lookup by name (for id) will be slower than a lookup by id.
//	Ids are montonely increasing by 1 and can be used to index values in an array.
//	The first Id is 0.
//	Id's should not be removed from the manager. However a manager can be cleared or locked.
//	On clear all mappings are lost, on lock no new mappings will be allowed until clear.
// ---------------------------------------------------------------------------------------------

class CIdMap
{	
public:
	
	BASELIB_API CIdMap(uint32 _ContainerSize=512);
	BASELIB_API ~CIdMap();
	
public:
	
	BASELIB_API IdType RegisterId(const char* _pName);
	IdType RegisterId(const std::string& _rName)		{ return RegisterId(_rName.c_str()); }
	
	IdType GetId(const char* _pName) const				{ return GetId(_pName, Math::CRC32(_pName)); }
	IdType GetId(const std::string& _rName) const		{ return GetId(_rName.c_str(), Math::CRC32(_rName)); }
	
	bool IsRegistered(const char* _pName) const			{ return GetId(_pName) != InvalidId; }
	bool IsRegistered(const std::string& _rName) const	{ return GetId(_rName) != InvalidId; }
	
	BASELIB_API const std::string& GetName(IdType _Id) const;
	
	BASELIB_API void Lock();
	BASELIB_API void Clear();
	
	uint32 Size() const									{ return m_IdLookUp.size(); }
	
public:
	
	struct IIdOp
	{
		void operator () (const CIdItem* _pItem);
		virtual void operator () (IdType CB_UNUSED(_Id)) {};
	};
	
	BASELIB_API void ForEachId(IIdOp& _rOperator) const;
	
private:
	
	BASELIB_API IdType GetId(const char* _pName, uint32 _Crc) const;
	
	uint32 GetSlot(const char* _pName)	const			{ return GetSlot(Math::CRC32(_pName)); }
	uint32 GetSlot(uint32 _Crc)	const					{ return _Crc & (m_ContainerSize-1); }
	
private:
	
	struct SSortIdOp
	{
		bool operator () (const CIdItem* _pLhs, const CIdItem* _pRhs);
	};
	
	typedef std::vector<CIdItem*>			CIdItemVector;
	typedef std::set<CIdItem*, SSortIdOp>	CIdItemSet;

private:
	
	bool			m_Locked;
	uint32			m_ContainerSize;
	CIdItemSet*		m_NameLookUp;
	CIdItemVector	m_IdLookUp;
};

CB_NAMESPACE_END

#endif
